package com.jiutong.meipenggang.ui.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jiutong.meipenggang.R;
import com.jiutong.meipenggang.base.BaseFragment;
import com.jiutong.meipenggang.common.Constants;
import com.jiutong.meipenggang.entity.MCBMainPageBean;
import com.jiutong.meipenggang.http.model.interfaces.MeiCaiBaoBiz;
import com.jiutong.meipenggang.http.present.MeiCaiBaoPresent;
import com.jiutong.meipenggang.ui.activitys.PayInActivity;
import com.jiutong.meipenggang.ui.activitys.PayOut2Activity;
import com.jiutong.meipenggang.utils.DensityUtil;
import com.jiutong.meipenggang.utils.SpConfig;
import com.jiutong.meipenggang.views.ScrollViewContainer;
import com.jiutong.meipenggang.views.TextChangeColorView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作用:
 * Created by 78 on 2017/9/27.
 */

public class MeiCaiBaoFragment extends BaseFragment implements MeiCaiBaoBiz {
    @Bind(R.id.sv_container1)
    ScrollViewContainer svContainer;
    @Bind(R.id.tv_zhuangru)
    TextView tvZhuangru;
    @Bind(R.id.tvzhuangchu)
    TextView tvzhuangchu;
    @Bind(R.id.ll_meicaibao_bottom)
    LinearLayout llMeicaibaoBottom;
    @Bind(R.id.mcb_year_earn)
    TextView mcbYearEarn;
    @Bind(R.id.mcb_money)
    TextView mcbMoney;
    @Bind(R.id.mcb_total_earn)
    TextView mcbTotalEarn;
    @Bind(R.id.line2)
    View line2;
    @Bind(R.id.vPager_meicaibao)
    ViewPager vPagerMeicaibao;
    @Bind(R.id.fl_hongbao_list)
    FrameLayout flHongbaoList;
    @Bind(R.id.ll_motlo)
    LinearLayout llMotlo;

    private ScrollViewContainer container;
    private boolean mHidden;
    private FragmentStatePagerAdapter pagerAdapter;
    private int width1_3;
    private int width;
    private int mCurrentIndex;
    private Display display;
    private Display display1;
    private DisplayMetrics outMetrics;
    private List<TextChangeColorView> mList = new ArrayList<>();
    private String[] str = new String[]{"产品介绍", "资产明细", "常见问题"};
    private TextChangeColorView tx;
    private int index;
    private MeiCaiBaoPresent meiCaiBaoPresent;
    private List<Fragment> fragmentList = new ArrayList<>();
    private Display display2;


    @Override
    protected int getLayoutResouceId() {
        return R.layout.fragment_meicaibao;
    }

    @Override
    protected void initView() {
        svContainer = new ScrollViewContainer(getContext());
        //获得屏幕宽度
        display1 = getActivity().getWindow().getWindowManager().getDefaultDisplay();
        outMetrics = new DisplayMetrics();
        display1.getMetrics(outMetrics);

        //定义下划线宽度为屏幕的三分之一
        width1_3 = outMetrics.widthPixels / 3;
        width = DensityUtil.dip2px(getContext(), 70);
        initIndicator();
        ProductIntroduceFragment productIntroduceFragment = new ProductIntroduceFragment();
        AssertDetailFragment assertDetailFragment = new AssertDetailFragment();
        CommonQuestionFragmeng commonQuestionFragmeng = new CommonQuestionFragmeng();
        fragmentList.add(productIntroduceFragment);
        fragmentList.add(assertDetailFragment);
        fragmentList.add(commonQuestionFragmeng);
        initViewPager();
    }

    private void initViewPager() {
        //初始下划线marginleft
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) line2.getLayoutParams();
        lp.width = width;
        lp.leftMargin = (width1_3 - width) / 2;
        line2.setLayoutParams(lp);
        pagerAdapter = new FragmentStatePagerAdapter(getFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        vPagerMeicaibao.setOffscreenPageLimit(3);
        vPagerMeicaibao.setAdapter(pagerAdapter);
        vPagerMeicaibao.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                //根据滑动的positionOffset设置leftMargin
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) line2.getLayoutParams();
                //0->1
                if (mCurrentIndex == 0 && position == 0) {
                    lp.leftMargin = (int) (mCurrentIndex * width1_3 + width1_3 * positionOffset) + (width1_3 - width) / 2;
                } //1->0
                else if (mCurrentIndex == 1 && position == 0) {
                    lp.leftMargin = (int) (mCurrentIndex * width1_3 + (positionOffset - 1) * width1_3) + (width1_3 - width) / 2;
                } //1->2
                else if (mCurrentIndex == 1 && position == 1) {
                    lp.leftMargin = (int) (mCurrentIndex * width1_3 + positionOffset * width1_3) + (width1_3 - width) / 2;
                } //2->1
                else if (mCurrentIndex == 2 && position == 1) {
                    lp.leftMargin = (int) (mCurrentIndex * width1_3 + (positionOffset - 1) * width1_3) + (width1_3 - width) / 2;
                }
                lp.width = width;
                line2.setLayoutParams(lp);

                TextChangeColorView left = mList.get(position);
                left.setmDirection(TextChangeColorView.Direction.RIGHT_TO_LEFT);
                left.setmCurrentProgress(1 - positionOffset);
                if (position == 2) {
                    return;
                }
                TextChangeColorView right = mList.get(position + 1);
                right.setmCurrentProgress(positionOffset);
                right.setmDirection(TextChangeColorView.Direction.LEFT_TO_RIGHT);

            }

            @Override
            public void onPageSelected(int position) {
                if (tx.isClickable()) {
                    TextChangeColorView tccvLast = mList.get(mCurrentIndex);
                    TextChangeColorView tccvCurrent = mList.get(position);

                    tccvLast.setChangeColor(getResources().getColor(R.color.hongbao_table_gray));
                    tccvLast.invalidate();

                    tccvCurrent.setChangeColor(getResources().getColor(R.color.gesture_color));
                    tccvCurrent.invalidate();
                }

                mCurrentIndex = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    protected void initData(Bundle arguments) {
        meiCaiBaoPresent = new MeiCaiBaoPresent(getActivity(), this);
        meiCaiBaoPresent.mianPage();
    }

    @Override
    public void onResume() {
        super.onResume();

        if (mHidden)
            return;
        doNetWork();
    }

    /**
     * 联网加载数据
     */
    private void doNetWork() {
        meiCaiBaoPresent.mianPage();
    }

    @OnClick({R.id.tv_zhuangru, R.id.tvzhuangchu})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_zhuangru:
                openActivity(PayInActivity.class);
                break;
            case R.id.tvzhuangchu:
                openActivity(PayOut2Activity.class);
                break;
        }
    }

    /**
     * 添加字体变色的样式
     */
    private void initIndicator() {
        for (index = 0; index < str.length; index++) {
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.weight = 1;
            tx = new TextChangeColorView(getContext());
            tx.setText(str[index]);
            tx.setChangeColor(getResources().getColor(R.color.register_bule));
            tx.setTextSize(20);
            tx.setLayoutParams(lp);
            llMeicaibaoBottom.addView(tx);
            mList.add(tx);
            final int index = this.index;
            //字体的点击监听
            tx.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    vPagerMeicaibao.setCurrentItem(index, true);
                    tx.setTextColor(getResources().getColor(R.color.register_bule));
//                    getFragmentPosition(index);
                }
            });
        }
    }

//    private void getFragmentPosition(int position) {
//        Fragment fragment = new HongbaoListFragment();
//        Bundle bundle = new Bundle();
//        bundle.putString(HongbaoListFragment.TYPE, position+"");
//        fragment.setArguments(bundle);
//    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        mHidden = hidden;
        if (!hidden) {
            doNetWork();
        }
    }

    @Override
    public void onSuccess(Object object) {
        MCBMainPageBean respone = (MCBMainPageBean) object;
        mcbYearEarn.setText(respone.getApr() + "");
        mcbMoney.setText(respone.getBtBalance() + "");
        mcbTotalEarn.setText(respone.getTotalIncome() + "");
        SpConfig.getInstance().edit().putString(Constants.ID, respone.getId().toString()).commit();
        SpConfig.getInstance().edit().putString(Constants.TOKEN, respone.getToken()).commit();
    }


    @Override
    public void onError(Throwable e) {

    }

    @Override
    public int getOpt() {
        return 0;
    }

    @Override
    public void onSuccess2(Object object) {

    }

    @Override
    public void onError2(Throwable e) {

    }
}
